package com.zuowei.ycy.user.ui.index;


import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;

import com.and.yzy.frame.util.RetrofitUtils;
import com.zuowei.ycy.R;
import com.zuowei.ycy.user.config.UserManger;
import com.zuowei.ycy.user.http.Collection;
import com.zuowei.ycy.user.http.Merchant;
import com.zuowei.ycy.ui.BaseFgt;
import com.zuowei.ycy.ui.BaseToolbarAty;
import com.zuowei.ycy.user.ui.login.LoginAty;
import com.zuowei.ycy.user.util.AppJsonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 * 商家所有信息tab
 */
public class BusinessTabAty extends BaseToolbarAty {

    @Bind(R.id.tab)
    TabLayout mTab;
    @Bind(R.id.view_pager)
    ViewPager mViewPager;

    private List<BaseFgt> mFragments;
    private List<String> mTabsString;


    private boolean isCollection;

    @Override
    public int getLayoutId() {
        return R.layout.index_business_tab_layout;
    }


    @Override
    public void initData() {




        mToolbar.setTitle(getIntent().getExtras().getString("shop_name", "外卖餐厅"));


        mFragments = new ArrayList<>();
        mTabsString = new ArrayList<>();
        mTabsString.add("商品");
        mTabsString.add("评价");
        mTabsString.add("商家");


        mFragments.add(new BusinessSortChooseFgt());
        mFragments.add(new BusinessPinJiaFgt());
        mFragments.add(new BusinessHomeFgt());


        mTab.addTab(mTab.newTab().setText(mTabsString.get(0)), true);
        mTab.addTab(mTab.newTab().setText(mTabsString.get(1)), false);
        mTab.addTab(mTab.newTab().setText(mTabsString.get(2)), false);


        pageAdapter pageAdapter = new pageAdapter(getSupportFragmentManager());
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setAdapter(pageAdapter);


        mTab.setupWithViewPager(mViewPager);


    }



    MenuItem menuItem;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_like, menu);
        menuItem = menu.getItem(0);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.menu_like) {

            if (!UserManger.isLogin()){

                startActivityForResult(LoginAty.class, null, 1);

            }else {
                if (!isCollection) {

                    showLoadingDialog(null);
                    doHttp(RetrofitUtils.createApi(Collection.class).addCollection(UserManger.getM_id(), getIntent().getExtras().getString("merchant_id")), 1);
                } else {
                    showLoadingDialog(null);
                    doHttp(RetrofitUtils.createApi(Collection.class).deleteCollection(UserManger.getM_id(), getIntent().getExtras().getString("merchant_id")), 2);
                }
            }

        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public boolean setIsInitRequestData() {
        return true;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1&&resultCode==RESULT_OK){
            requestData();
        }
    }

    @Override
    public void requestData() {

        String merchant_id=getIntent().getExtras().getString("merchant_id");
        String m_id= UserManger.getM_id();
        doHttp(RetrofitUtils.createApi(Merchant.class).merchantInfo(merchant_id, m_id), 0);

    }

    class pageAdapter extends FragmentStatePagerAdapter {
        public pageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTabsString.get(position);
        }

    }


    @Override
    public void onSuccess(String result, Call<ResponseBody> call, Response<ResponseBody> response, int what) {

        switch (what) {
            case 0:
                String is_collection= AppJsonUtil.getString(result,"is_collection");

                if (is_collection.equals("1")) {
                    isCollection=true;
                    menuItem.setIcon(getResources().getDrawable(R.drawable.yishoucang));
                } else {
                    isCollection=false;
                    menuItem.setIcon(getResources().getDrawable(R.drawable.jiarushoucang));
                }
                break;

            case 1:
                showToast("收藏成功");
                isCollection=true;
                menuItem.setIcon(getResources().getDrawable(R.drawable.yishoucang));
                break;
            case 2:
                showToast("取消收藏成功");
                isCollection=false;
                menuItem.setIcon(getResources().getDrawable(R.drawable.jiarushoucang));
                break;

        }


        super.onSuccess(result, call, response, what);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        // super.onSaveInstanceState(outState);
    }
}
